<?php
/* SVN FILE: $Id: inventory.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Viewing your inventory
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/inventory/inventory.php $
*/

class public_ibmarket_inventory_inventory extends marketPublicCommand
{
	public static $orderBy = array(
									'ASC' => 1,
									'DESC' => -1,
								);
	public static $orderDefault = array(
										'item' => 'ASC',
										'icon' => 'ASC',
										'name' => 'ASC',
										'category' => 'ASC',
										'time' => 'DESC',
										'expire' => 'DESC',
										'resell' => 'DESC',
										'price' => 'ASC',
									);
	
	public function doExecute( ipsRegistry $registry )
	{
		if($this->memberData['member_id'] == 0 || $this->memberData['g_market_use'] == 0)
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$this->request['order'] = isset(self::$orderDefault[$this->request['order']]) ? $this->request['order'] : 'time';
		$this->request['by'] = isset(self::$orderBy[$this->request['by']]) ? $this->request['by'] : self::$orderDefault[$this->request['order']];
		
		$inventoryItems = marketRegistry::getInventoryItem($this->memberData['member_id']);
		usort($inventoryItems, array(self,'compareItemByInput'));
		
		self::$orderDefault[$this->request['order']] = self::$orderDefault[$this->request['order']] == 'ASC' ? 'DESC' : 'ASC';
		$content = $this->registry->getClass('output')->getTemplate('ibmarket')->inventoryPage($inventoryItems,self::$orderDefault);
		
		$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
		
		$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
		$this->registry->getClass('output')->addContent( $template );
		
        $this->registry->getClass('output')->sendOutput();
	}
	
	public static function compareItemByInput($a, $b)
	{
		$caches =& ipsRegistry::cache()->fetchCaches();
		$request =& ipsRegistry::fetchRequest();
		switch($request['order'])
		{
			case 'item':
				if($a['item'] < $b['item'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['item'] > $b['item'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'icon':
				if(($c = strcasecmp($caches['market_items'][$a['item']]['icon'],$caches['market_items'][$b['item']]['icon'])) != 0)
				{
					return $c * self::$orderBy[$request['by']];
				}
				break;
			case 'name':
				if(($c = strcasecmp($caches['market_items'][$a['item']]['item_name'],$caches['market_items'][$b['item']]['item_name'])) != 0)
				{
					return $c * self::$orderBy[$request['by']];
				}
				break;
			case 'category':
				$order = marketRegistry::getCategoryOrder();
				$order = array_flip($order);
				if($order[$caches['market_items'][$a['item']]['category']] < $order[$caches['market_items'][$b['item']]['category']])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($order[$caches['market_items'][$a['item']]['category']] > $order[$caches['market_items'][$b['item']]['category']])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'time':
				if($a['time_bought'] < $b['time_bought'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['time_bought'] > $b['time_bought'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'expire':
				if($a['expires'] != $b['expires'])
				{
					if($a['expires'] < $b['expires'] || $b['expires'] == 0)
					{
						return -1 * self::$orderBy[$request['by']];
					}
					elseif($a['expires'] > $b['expires'] || $a['expires'] == 0)
					{
						return 1 * self::$orderBy[$request['by']];
					}
				}
				break;
			case 'resell':
				if($a['resell_price'] < $b['resell_price'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['resell_price'] > $b['resell_price'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
			case 'price':
				if($a['price_bought'] < $b['price_bought'])
				{
					return -1 * self::$orderBy[$request['by']];
				}
				elseif($a['price_bought'] > $b['price_bought'])
				{
					return 1 * self::$orderBy[$request['by']];
				}
				break;
		}
		return self::compareItemDefault($a, $b);
	}
	
	public static function compareItemDefault($a, $b)
	{
		$caches =& ipsRegistry::cache()->fetchCaches();
		if($a['time_bought'] < $b['time_bought']) // Later bought first
		{
			return 1;
		}
		elseif($a['time_bought'] > $b['time_bought'])
		{
			return -1;
		}
		if($a['expires'] != $b['expires']) // Expires sooner first
		{
			if($a['expires'] < $b['expires'] || $b['expires'] == 0)
			{
				return -1;
			}
			elseif($a['expires'] > $b['expires'] || $a['expires'] == 0)
			{
				return 1;
			}
		}
		if($a['price_bought'] < $b['price_bought']) // Cheapest item first
		{
			return -1;
		}
		elseif($a['price_bought'] > $b['price_bought'])
		{
			return 1;
		}
		if($a['resell_price'] < $b['resell_price']) // Higher resell value first
		{
			return 1;
		}
		elseif($a['resell_price'] > $b['resell_price'])
		{
			return -1;
		}
		$order = marketRegistry::getCategoryOrder();
		$order = array_flip($order);
		if($order[$caches['market_items'][$a['item']]['category']] < $order[$caches['market_items'][$b['item']]['category']]) // First order category first
		{
			return -1;
		}
		elseif($order[$caches['market_items'][$a['item']]['category']] > $order[$caches['market_items'][$b['item']]['category']])
		{
			return 1;
		}
		if(($c = strcasecmp($caches['market_items'][$a['item']]['item_name'],$caches['market_items'][$b['item']]['item_name'])) != 0) // Item name by alphabetical order
		{
			return $c;
		}
		if(($c = strcasecmp($caches['market_items'][$a['item']]['icon'],$caches['market_items'][$b['item']]['icon'])) != 0) // Icon by alphabetical order
		{
			return $c;
		}
		if($a['item'] < $b['item']) // Smaller item ID first
		{
			return -1;
		}
		elseif($a['item'] > $b['item'])
		{
			return 1;
		}
		if($a['i_id'] < $b['i_id']) // Smaller inventory item ID first
		{
			return -1;
		}
		elseif($a['i_id'] > $b['i_id'])
		{
			return 1;
		}
		return 0; // Should never right here
	}
}